package com.lovo.javafxComponent;

import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;

import java.io.*;

/**
 * 图片选择器
 * @author lenovo
 *
 */
public class LovoImageFileChooser {
	/**文件选择器*/
	private FileChooser fileChooser = new FileChooser();
	/**选择的文件*/
	private File selectFile;
	/**预览图片*/
	private LovoImage img;
	/**错误提示标签*/
	private Label infoLabel = new Label("");
	/**是否通过验证*/
	private boolean pass;

	/**
	 * 构造方法
	 * @param x X坐标
	 * @param y Y坐标
	 * @param width 宽度
	 * @param height 高度
	 * @param container 加入容器
	 */
	public LovoImageFileChooser(int x,int y,int width,int height,Pane container) {
		fileChooser.setTitle("选择图片");
		//设置过滤
		fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("image", "*.jpg","*.png","*.gif")
        );
		
		img = new LovoImage(null, x+10, y, width-20, height-30, container, true);
		LovoButton selectButton = new LovoButton("选择图片",
				x+(width-100)/2, y+height-25, container);
		
		selectButton.setOnAction(e -> {
			selectFile = fileChooser.showOpenDialog(null);
			if(selectFile != null) {
				img.flushImage("file:"+selectFile.getAbsolutePath());

				pass = true;
				infoLabel.setTextFill(new Color(0,0,1,1));
				infoLabel.setText("验证通过");
			}
		});

		infoLabel.setFont(new Font(12));
		infoLabel.setLayoutX(x+(width-100)/2+20);
		infoLabel.setLayoutY(y+height);
		container.getChildren().add(infoLabel);
	}
	public LovoImageFileChooser(String url,int x,int y,int width,int height,Pane container) {
		this(x,y,width,height,container);
		img = new LovoImage(url, x+10, y, width-20, height-30, container, true);

	}
	
	/**
	 * 得到选择文件路径
	 * @return 选择文件路径
	 */
	public String getSelectFilePath() {
		if(selectFile != null) {
			return selectFile.getAbsolutePath();
		}
		return null;
	}

	/**
	 * 保存文件
	 * @param dirPath 存放文件目录
	 * @return 重命名文件
	 */
	public String saveFile(String dirPath){
		InputStream in = null;
		OutputStream out = null;

		try {
			String filePath = selectFile.getAbsolutePath();
			String fileName = System.currentTimeMillis()+
					filePath.substring(filePath.lastIndexOf("."));

			String url = dirPath.replace("\\","/");

			if ( url.length() != -1) {
				String pathUrl = Thread.currentThread().getContextClassLoader()
						.getResources(url).nextElement().toString();
				url = pathUrl.substring(6);
			}


			in = new FileInputStream(filePath);
			out = new FileOutputStream(url+ "/"+fileName);

			byte[] by = new byte[1024];
			int len = 0;
			while( (len = in.read(by)) != -1){
				out.write(by,0,len);
			}

			return fileName;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}


		return null;
	}

	private void checkImg(){
		if(this.selectFile == null){
			pass = false;
			infoLabel.setTextFill(new Color(1,0,0,1));
			infoLabel.setText("请选择图片");
		}
	}

	/**
	 * 是否通过验证
	 * @return
	 */
	public boolean isPass(){
		checkImg();
		return this.pass;
	}
}
